package com.dabaozhan.modules.wx_app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ValidationBeanUtils;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.BookBillEntity;
import com.dabaozhan.modules.wx_app.entity.BookBillQueryEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.BookBillFacade;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "手支记账")
@RestController
@RequestMapping("/wx_app")
public class BookBillController {
	
private static final Logger LOGGER = LoggerFactory.getLogger(BookBillController.class);
	
	@Autowired
	private BookBillFacade bookBillFacade;

	@ApiOperation(value = "查询记账列表")
    @RequestMapping(value = "/getBill")
	public String getBill(BookBillQueryEntity entity){
		ResponseDto<List<BookBillEntity>> responseDto = new ResponseDto<List<BookBillEntity>>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			PageResult<List<BookBillEntity>> pageResult = bookBillFacade.getBookBillList(entity);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(pageResult.getListData());
			responseDto.setTotal(pageResult.getTotal());
			responseDto.setPage(entity.getPage());
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询记账列表发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询记账列表请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	
	@ApiOperation(value = "添加记账")
    @RequestMapping(value = "/addBill")
	public String addBill(BookBillEntity entity,HttpServletRequest request){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			//图片保存路径
			String pathUrl = request.getSession().getServletContext().getRealPath("/")+ ("fileImage/");
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			bookBillFacade.addBookBill(entity, pathUrl);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("添加记账发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("添加记账请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
}
